﻿using CoreModule;
using CoreModule.Storages;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Controls;
namespace IdResourceModule.Views
{
    /// <summary>
    /// PatientStatisticsControl.xaml 的交互逻辑
    /// </summary>
    public partial class PatientStatisticsControl : UserControl, INotifyPropertyChanged
    {
        private int todayNewPatientNum;
        public int TodayNewPatientNum { get { return this.todayNewPatientNum; } set { this.todayNewPatientNum = value; OnPropertyChanged(nameof(TodayNewPatientNum)); } }
        private int totalPatientNum;
        public int TotalPatientNum { get { return this.totalPatientNum; } set { this.totalPatientNum = value; OnPropertyChanged(nameof(TotalPatientNum)); } }
        CoreModule.Storages.DbStorage DbStorage { get; set; }
        public PatientStatisticsControl(CoreModule.Storages.DbStorage dbStorage, IDeviceManager deviceManager)
        {
            InitializeComponent();
            DataContext = this;
            DbStorage = dbStorage;
            try
            {
                RefreshData();
            } catch (Exception e)
            {
            }
            deviceManager.MeasurementResultCompletedInManagementModule += DeviceManager_MeasurementResultCompletedInManagementModule;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeviceManager_MeasurementResultCompletedInManagementModule(object sender, MeasurementResultInfoEventArgs e)
        {
            RefreshData();
        }
         
        public void RefreshData()
        {
                TodayNewPatientNum = DbStorage.PatientInfos.Count(it => it.CreateTime.Date == DateTime.Now.Date);
                TotalPatientNum = DbStorage.PatientInfos.Count();
          
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
